package com.hyfrogx.modules.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyfrogx.common.base.BaseServiceImpl;
import com.hyfrogx.common.base.QueryForm;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.modules.sys.form.dict.detail.DictDetailVo;
import com.hyfrogx.modules.sys.service.SysDictService;
import com.hyfrogx.modules.sys.dao.SysDictDetailDao;
import com.hyfrogx.modules.sys.entity.SysDictDetailEntity;
import com.hyfrogx.modules.sys.entity.SysDictEntity;
import com.hyfrogx.modules.sys.service.SysDictDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典详情 服务实现类
 * </p>
 *
 * @author wanglei
 * @date 2019-07-15 15:51:11
 */
@Service
public class SysDictDetailServiceImpl extends BaseServiceImpl<SysDictDetailDao, SysDictDetailEntity> implements SysDictDetailService {

	@Autowired
    SysDictService dictService;

	@Override
	public String getIdKey() {
		return "id";
	}

	@Override
	public String getEntityName() {
		return "字典详情";
	}

	@Override
	public List<String> getUpdateIgnoreKey() {
		return CollUtil.newArrayList("code");
	}

	@Override
	public void beforeSaveOrUpdateEntity(SysDictDetailEntity entity, boolean isNew) {
		super.beforeSaveOrUpdateEntity(entity, isNew);
		if (retBool(this.count(
				new QueryWrapper<SysDictDetailEntity>()
						.eq("code", entity.getCode())
						.ne(!isNew, "id", entity.getId())
						.eq("dict_id", entity.getDictId())
		))) {
			throw new MyException("字典详情代码已存在");
		}
		if (retBool(this.count(
				new QueryWrapper<SysDictDetailEntity>()
						.eq("name", entity.getName())
						.ne(!isNew, "id", entity.getId())
						.eq("dict_id", entity.getDictId())
		))) {
			throw new MyException("字典详情名称已存在");
		}
	}

	@Override
	public QueryWrapper<SysDictDetailEntity> getSelectWrapper(QueryForm<SysDictDetailEntity> form, boolean isOrder) {
		String name = form.getParamStr("name");
		String code = form.getParamStr("code");
		Long dictId = form.getParamLong("dictId");
		Integer status = form.getParamInteger("status");
		Integer isAll = form.getParamInteger("isAll");
		return form.getWrapper(isOrder)
				.eq(ObjectUtil.isNotNull(dictId), "dict_id", dictId)
				.like(StrUtil.isNotBlank(name), "name", name)
				.like(StrUtil.isNotBlank(code), "code", code)
				.eq(ObjectUtil.notEqual(isAll, 1) && ObjectUtil.isNotNull(status), "status", status)
				;
	}

	@Override
	public List<DictDetailVo> selectByDictCode(String dictCode) {
		SysDictEntity dict = dictService.getByCode(dictCode);
		return selectByDict(dict);
	}

	@Override
	public List<DictDetailVo> selectByDictId(Long dictId) {
		SysDictEntity dict = dictService.getById(dictId);
		return selectByDict(dict);
	}

	@Override
	public boolean hasCode(String dictCode, String detailCode) {
		List<DictDetailVo> dictDetailVos = this.selectByDictCode(dictCode);
		DictDetailVo vo = CollUtil.findOneByField(dictDetailVos, "code", detailCode);
		return ObjectUtil.isNotNull(vo);
	}


	/**
	 * 通过字典获取详情
	 *
	 * @param dict
	 * @return
	 */
	private List<DictDetailVo> selectByDict(SysDictEntity dict) {
		if (ObjectUtil.isNull(dict)) {
			throw new MyException("查找字典不存在");
		}
		if (!BooleanUtil.isTrue(dict.getStatus())) {
			throw new MyException("查找字典已禁用");
		}
		List<SysDictDetailEntity> list = this.list(
				new QueryWrapper<SysDictDetailEntity>()
						.eq("dict_id", dict.getId())
						.eq("status", 1)
						.orderByDesc("order_num")
		);
		return this.convertToListVo(list, DictDetailVo.class);
	}
}
